<template>
  <div class="app-container">
    <!-- 非管理后台页面才显示导航栏 -->
    <template v-if="!isAdminRoute">
      <nav class="navbar" :class="{ 'navbar-collapsed': isCollapsed }" aria-label="主导航">
        <button class="collapse-btn" @click="toggleCollapse" v-if="!isMobile || isCollapsed">
          {{ isCollapsed ? '展开' : '折叠' }}
        </button>
        <router-link to="/" class="nav-item" :class="{ active: isActive('/') }">
          <span class="icon">🏠</span>
          <span class="text" v-if="!isCollapsed && !isMobile">首页</span>
        </router-link>
        <router-link to="/categories" class="nav-item" :class="{ active: isActive('/categories') }">
          <span class="icon">🛍️</span>
          <span class="text" v-if="!isCollapsed && !isMobile">商品分类</span>
        </router-link>
        <router-link to="/Order/Order" class="nav-item" :class="{ active: isActive('/Order') }">
          <span class="icon">📦</span>
          <span class="text" v-if="!isCollapsed && !isMobile">我的订单</span>
        </router-link>
        <router-link to="/Cart/Cart" class="nav-item" :class="{ active: isActive('/Cart/Cart') }">
          <span class="icon">🛒</span>
          <span class="text" v-if="!isCollapsed && !isMobile">购物车</span>
        </router-link>
        <router-link to="/Profile" class="nav-item" :class="{ active: isActive('/Profile') }">
          <span class="icon">👤</span>
          <span class="text" v-if="!isCollapsed && !isMobile">个人中心</span>
        </router-link>
        <router-link to="/admin" class="nav-item admin-link" :class="{ active: isActive('/admin') }">
          <span class="icon">⚙️</span>
          <span class="text" v-if="!isCollapsed && !isMobile">卖家后台</span>
        </router-link>
      </nav>
      <div class="main-content" :class="{ 'content-collapsed': isCollapsed }">
        <div class="content-wrapper">
          <router-view></router-view>
        </div>
      </div>
    </template>

    <!-- 管理后台页面直接显示路由内容 -->
    <template v-else>
      <router-view></router-view>
    </template>

    <div v-if="showAddToCartNotification" class="notification">
      商品已添加到购物车
    </div>
  </div>
</template>

<script setup>
import { ref, computed, watch } from 'vue';
import { useRoute } from 'vue-router';
import { useCartStore } from './stores/cartStore';

const route = useRoute();
const cartStore = useCartStore();
const isCollapsed = ref(false);
const showAddToCartNotification = ref(false);
const isMobile = ref(window.innerWidth <= 768);

// 判断是否是管理后台路由
const isAdminRoute = computed(() => {
  return route.path.startsWith('/admin');
});

watch(() => cartStore.totalItems, () => {
  showAddToCartNotification.value = true;
  setTimeout(() => {
    showAddToCartNotification.value = false;
  }, 2000);
});

const isActive = (path) => {
  if (path === '/') {
    return route.path === '/' || route.path.toLowerCase() === '/index';
  }
  return route.path.startsWith(path);
};

const toggleCollapse = () => {
  isCollapsed.value = !isCollapsed.value;
};

// 监听窗口大小变化
window.addEventListener('resize', () => {
  isMobile.value = window.innerWidth <= 768;
});
</script>

<style scoped>
.app-container {
  min-height: 100vh;
  width: 100%;
}

.main-content {
  margin-left: 200px;
  transition: margin-left 0.3s ease;
  width: calc(100% - 200px);
}

.main-content.content-collapsed {
  margin-left: 50px;
  width: calc(100% - 50px);
}

.content-wrapper {
  padding: 20px;
  height: 100vh;
  overflow-y: auto;
  scrollbar-width: none; /* Firefox */
  -ms-overflow-style: none; /* IE and Edge */
}

.content-wrapper::-webkit-scrollbar {
  display: none; /* Chrome, Safari and Opera */
}

.navbar {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: 200px;
  background-color: #fdf7e3;
  display: flex;
  flex-direction: column;
  padding: 20px 10px;
  transition: all 0.3s ease;
  z-index: 1000;
}

.navbar-collapsed {
  width: 50px;
}

.collapse-btn {
  display: none;
  padding: 8px 15px;
  margin-bottom: 20px;
  background-color: #f3c223;
  color: #333;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  transition: all 0.3s ease;
  width: 100%;
}

.collapse-btn:hover {
  background-color: #f0b400;
}

.nav-item {
  display: flex;
  align-items: center;
  text-decoration: none;
  color: #000;
  font-size: 16px;
  padding: 10px;
  transition: all 0.3s ease;
  border-radius: 5px;
  text-align: center;
}

.nav-item .icon {
  margin-right: 5px;
}

.nav-item:hover,
.nav-item.active {
  background-color: #f3c223;
  color: white;
}

.notification {
  position: fixed;
  bottom: 60px;
  right: 20px;
  background-color: #4caf50;
  color: white;
  padding: 10px 20px;
  border-radius: 4px;
  z-index: 1000;
}

.admin-link {
  margin-top: auto;
  border-top: 1px solid rgba(0, 0, 0, 0.1);
  margin-top: 20px;
  padding-top: 20px;
}

@media (max-width: 768px) {
  .main-content {
    margin-left: 0;
    width: 100%;
    padding-bottom: 60px;
  }

  .navbar {
    top: auto;
    bottom: 0;
    width: 100%;
    height: auto;
    flex-direction: row;
    justify-content: space-around;
    padding: 10px 0;
  }

  .content-wrapper {
    height: calc(100vh - 60px);
  }

  .admin-link {
    margin-top: 0;
    border-top: none;
    padding-top: 10px;
  }
}

@media (min-width: 769px) {
  .collapse-btn {
    display: block;
  }

  .navbar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: 200px;
    background-color: #fdf7e3;
    display: flex;
    flex-direction: column;
    padding: 20px 10px;
    transition: all 0.3s ease;
    z-index: 1000;
  }

  .navbar-collapsed {
    width: 50px;
    .nav-item {
      justify-content: center;
      .icon {
        margin-right: 0;
      }
    }
  }

  .main-content {
    margin-left: 200px;
    width: calc(100% - 200px);
    transition: all 0.3s ease;
  }

  .main-content.content-collapsed {
    margin-left: 50px;
    width: calc(100% - 50px);
  }
}
</style>
